<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item
          :hidden="true"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
          />
        </a-form-item>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="合同订单编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="合同订单编号"
                :disabled="editable || showable"
                v-decorator="['orderId', {rules: [{required: true, message: '请输入合同订单编号'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="产品名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <material-select-madal :disabled="editable || showable" v-decorator="['productCode',{rules: [{required: true, message: '请选择产品名称'}]}]" title='选择产品'></material-select-madal>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="产线名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <lines-select-madal v-decorator="['linesCode',{rules: [{required: true, message: '请选择产线名称'}]}]" title='选择产线'></lines-select-madal>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="计划数量"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                placeholder="计划数量"
                :disabled="showable"
                v-decorator="['productCount', {rules: [{required: true, message: '请输入计划数量'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :getter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="计划开始时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-date-picker
                placeholder="计划开始时间"
                format="YYYY-MM-DD HH:mm:ss"
                :disabled="showable"
                show-time
                v-decorator="['planStartDate', {rules: [{required: true, message: '请选择计划开始时间'}]}]">
              </a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
          </a-col>
        </a-row>

      </a-form>

    </a-spin>
  </a-modal>

</template>
<script>
import pick from 'lodash.pick'
import { FormMixin } from '@/mixins/FormMixin'
import { getObj, addObj, putObj } from '@/api/plan/playday'
import materialSelectMadal from '@/views/main/base/material/materialSelectMadal'
import linesSelectMadal from '@/views/main/base/lines/linesSelectMadal'
import moment from 'moment'

export default {
  name: 'planDayForm',
  mixins: [FormMixin],
  components: {
    materialSelectMadal,
    linesSelectMadal
  },
  data () {
    return {
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },

  methods: {

    // 获取用户信息
    edit (id, type) {
      this.visible = true
      const { form: { setFieldsValue, resetFields } } = this
      resetFields()
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          record.planStartDate = moment(record.planStartDate,"YYYY-MM-DD HH:mm:ss")
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'orderId', 'productCode','linesCode','productCount','planStartDate']))
          })
        }).catch((err) => {
          this.$message.error(err.msg)
        }).finally(_ => {
          this.confirmLoading = false
        })
      }
    },



    // 结束保存用户信息
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          values.planStartDate = values.planStartDate.format('YYYY-MM-DD HH:mm:ss')
          console.log(values,'value')
          try {
            if (this.type === 'add') {
              await addObj(values)
            } else if (this.type === 'edit') {
              await putObj(values)
            }
            this.confirmLoading = false
            this.handleCancel()
            this.$emit('ok')
          } catch (e) {
            this.$message.error(e.msg)
            this.confirmLoading = false
          }
        }
      })
    }

  }
}
</script>

<style scoped>

</style>